<template>
  <div class="app-container">
    <!-- v-permission="['data:person:read']"
    v-permission="['data:person:create']"
    v-permission="['data:person:update']"
    v-permission="['data:person:delete']" -->
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model.trim="listQuery.fuserno" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户编号"/>
      <el-input v-model.trim="listQuery.fusername" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名称"/>
      <el-select v-model="listQuery.fsiteNo" clearable filterable class="filter-item" placeholder="请选择所属区/学校">
        <el-option
          v-for="item in fsiteNoBelongOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" :style="{background:buttonColor.add ,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
      <el-table-column align="center" label="用户编号" prop="fuserno"/>
      <el-table-column align="center" label="用户姓名" prop="fusername"/>
      <el-table-column :formatter="formatSite" align="center" label="所属区/学校" prop="fsiteNo"/>
      <el-table-column align="center" label="状态" prop="fstate">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.fstate | statusFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="职务" prop="fzw"/>
      <el-table-column align="center" label="备注" prop="fmemo"/>
      <el-table-column :formatter="dateFormat" align="center" label="修改时间" prop="fupddate"/>
      <!--      <el-table-column align="center" label="操作员" prop="foperator"/>-->

      <el-table-column align="center" width="140" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button :style="{background:buttonColor.edit,color:buttonColor.color}" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户编号" prop="fuserno">
          <el-input v-model="dataForm.fuserno"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="fusername">
          <el-input v-model="dataForm.fusername"/>
        </el-form-item>
        <el-form-item label="所属区/学校" prop="fsiteNo">
          <el-select v-model="dataForm.fsiteNo" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in fsiteNoBelongOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item class="el-form-item-small" label="状态" prop="fstate">
          <el-select v-model="dataForm.fstate" style="width: 200px" class="filter-item" placeholder="请选择状态">
            <el-option v-for="(key, value) in statusMap" :key="key" :label="key" :value="value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="职务" prop="fzw">
          <el-input v-model="dataForm.fzw"/>
        </el-form-item>
        <el-form-item label="备注" prop="fmemo">
          <el-input v-model="dataForm.fmemo"/>
        </el-form-item>
        <!--<el-form-item label="操作员" prop="foperator">
          <el-input v-model="dataForm.foperator"/>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPerson, createPerson, updatePerson, deletePerson } from '@/api/person'
import { fsiteNoBelongOptions } from '@/api/role'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import {buttonColor} from '@/filters/index'

const statusMap = {
  0: '禁用',
  1: '启用'
}

export default {
  name: 'Person',
  components: { Pagination },
  filters: {
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      fsiteNoBelongOptions: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fusername: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      statusMap,
      dataForm: {
        id: undefined,
        fuserno: undefined,
        fusername: undefined,
        fsiteNo: undefined,
        fstate: undefined,
        fzw: undefined,
        fmemo: undefined,
        fupddate: undefined,
        foperator: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fusername: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' }
        ]
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  created() {
    this.getList()
    fsiteNoBelongOptions()
      .then(response => {
        this.fsiteNoBelongOptions = response.data.data.list
      })
  },
  methods: {
    formatStatus: function(row, column) {
      return row.fstate === 1 ? '启用' : '禁用'
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    formatSite: function(row, column) {
      for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
        if (row.fsiteNo === this.fsiteNoBelongOptions[i].value) {
          return this.fsiteNoBelongOptions[i].label
        }
      }
      return ''
    },
    getList() {
      this.listLoading = true
      listPerson(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fuserno: undefined,
        fusername: undefined,
        fsiteNo: undefined,
        fstate: undefined,
        fzw: undefined,
        fmemo: undefined,
        fupddate: undefined,
        foperator: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createPerson(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加用户成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dataForm.fstate = row.fstate.toString()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatePerson(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新用户成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该用户资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePerson(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除用户资料成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.msg
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
